function a(Id) {
    let n = Id.value;
    console.log(n);
}

function Ajax() {
    var uname = document.getElementsByName('uname')[0];
    var tishi = document.getElementById('tishi');
    var req = new XMLHttpRequest();
    req.open('GET', 'http://localhost:8080/v2/user/check_user_name?uname=' + uname.value);
    req.send();
    req.onload = () => {
        if (req.responseText === 'exists') {
            tishi.innerHTML = '已经占用'
        } else if (req.responseText === 'non-exists') {
            tishi.innerHTML = '未被占用'
        }
    }
}

function Remove() {
    $('select').change(() => {
        $('#list').children().remove();
        Ajax_list();
    });
}

function Ajax_list() {
    //第一步，创建XMLHttpRequest对象
    var xmlHttp = new XMLHttpRequest();

    //第二步，调用xmlHttp的方法
    function CommentAll(method, url) {
        xmlHttp.onreadystatechange = callback;
        xmlHttp.open(method, url);
        xmlHttp.send();
        //插入加载动画
        $("tbody").append('<tr><th colspan="4" align="center"><div class="loader">Loading...</div></th></tr>')
    }

    //第三步，创建回调函数
    function callback() {
        if (xmlHttp.readyState == 4)
            if (xmlHttp.status == 200) {
                //取得返回的数据
                let data = xmlHttp.responseText;
                //json字符串转为json格式
                data = eval('(' + data + ')');
                if (data.length < $('select').val()) {
                    console.log('end')
                    $("#PgDn").attr('disabled', true);
                }
                //移除之前内容
                $('tbody').children().remove();
                $.each(data,
                    function (i, v) {
                        console.log(i, v);
                        $('#list').append(`<tr><th>${v.lid}</th><th>${v.title}</th><th>${v.price}</th><th><button class="d_list">删除</button><button class="s_list">查找</button></th></tr>`);
                    }
                )
                //绑定(删除的按钮)监听删除的事件
                $('tbody').find("button[class='d_list']").each(function () {
                        console.log('成功');
                        $(this).on(
                            "click", function () {
                                Ajax_Delete(this);
                            })
                    }
                )
            }
    }

    CommentAll('get', `http://localhost:8080/v2/product/list?dataPageSize=${$("select").val()}&page=${parseInt($('#Pg>input').val()) - 1}`);
}

function Ajax_Delete(Del) {
    var xmlHttp = new XMLHttpRequest();

    function CommentAll(method, url) {
        xmlHttp.onreadystatechange = callback;
        xmlHttp.open(method, url);
        xmlHttp.send();
    }

//第五步，创建回调函数
    function callback() {
        if (xmlHttp.readyState == 4)
            if (xmlHttp.status == 200) {
                //取得返回的数据
                let data = xmlHttp.responseText;
                //判断是否成功
                if (data === 'deleted') {
                    //删除他自己
                    $(Del).parent('th').parent('tr').remove();
                } else if (data === 'false') {
                    alert("删除失败");
                }
            }
    }

    CommentAll('delete', "http://localhost:8080/v2/product/L_delete?D_lid=" + $(Del).parent('th').siblings('th:first').text());
}

function Package_Select() {
    $('#PgUp').on('click', function () {
        if ($("#PgDn").is('[disabled]')) {
            console.log("删除下")
            $("#PgDn").removeAttr("disabled");
        }
        if (parseInt($('#Pg>input').val()) > 1) {
            $('#Pg>input').val(parseInt($('#Pg>input').val()) - 1);
            Ajax_list()
        } else {
            $("#PgUp").attr('disabled', true)
        }
    })
    $('#PgDn').on('click', function () {
        if ($("#PgUp").is('[disabled]')) {
            console.log("删除上")
            $("#PgUp").removeAttr("disabled");
        }
        $('#Pg>input').val(parseInt($('#Pg>input').val()) + 1);
        Ajax_list()
    })
}

function add_Goods() {
    $.post(
        "/v2/product/add",
        {
            Title: $("#title").val(),
            price: $("#price").val()
        },
        function (data,status) {
            console.log(data,status);
        }
    )
    // new Vue().$http.post(url, data).then(
    //     function (response) {
    //     console.log(response.body.message,response.status)
    //     }
    // ).catch(function(e){
    //         console.log(e);
    // });
}